package utils

import (
	"strconv"
	"strings"
	"unicode"

	huge "github.com/dablelv/go-huge-util"
	uuid "github.com/satori/go.uuid"
)

func If(flag bool, a, b interface{}) interface{} {
	if flag {
		return a
	}
	return b
}
func StrToInt64(str string) int64 {
	//数据解析
	val, _ := strconv.ParseInt(str, 10, 64)
	//结果
	return val
}
func Int64ToStr(value int64) string {
	//数据解析
	val := strconv.FormatInt(value, 10)
	//结果
	return val
}
func StrToInt(value string) int {
	//数据解析
	val, _ := strconv.Atoi(value)
	//结果
	return val
}
func IntToStr(value int) string {
	//数据解析
	val := strconv.Itoa(value)
	//结果
	return val
}

// MergeStringArray 合并数组
func MergeStringArray(dest []string, src []string) (result []string) {
	result = make([]string, len(dest)+len(src))
	//将第一个数组传入result
	copy(result, dest)
	//将第二个数组接在尾部，也就是 len(dest):
	copy(result[len(dest):], src)
	return
}
func MergeArray(dest []interface{}, src []interface{}) (result []interface{}) {
	result = make([]interface{}, len(dest)+len(src))
	//将第一个数组传入result
	copy(result, dest)
	//将第二个数组接在尾部，也就是 len(dest):
	copy(result[len(dest):], src)
	return
}

// DeleteIndex 删除数组
func DeleteIndex(src []interface{}, index int) (result []interface{}) {
	// 删除 也就是将该之前与之后的数组合并成新数组
	result = append(src[:index], src[(index+1):]...)
	return
}

func hugeSimple() {
	huge.UniqueIntSlice([]int{1, 2, 2, 3})            // [1 2 3]
	huge.UniqueUintSlice([]uint{1, 2, 2, 3})          // [1 2 3]
	huge.UniqueStrSlice([]string{"a", "b", "b", "c"}) // [a b c]

	huge.ReverseIntSlice([]int{1, 2, 3})          // [3 2 1]
	huge.ReverseUintSlice([]uint{1, 2, 3})        // [3 2 1]
	huge.ReverseStrSlice([]string{"a", "b", "c"}) // [c b a]

	huge.SumSlice([]int{1, 2, 3})           // 6
	huge.SumSlice([]uint{1, 2, 3})          // 6
	huge.SumSlice([]float32{1.1, 2.2, 3.3}) // 6.6
	huge.SumSlice([]float64{1.1, 2.2, 3.3}) // 6.6

	huge.JoinSliceWithSep([]int{1, 2, 3}, ",")           // 1,2,3
	huge.JoinSliceWithSep([]uint{1, 2, 3}, ",")          // 1,2,3
	huge.JoinSliceWithSep([]float64{1.1, 2.2, 3.3}, ",") // 1.1,2.2,3.3
	huge.JoinSliceWithSep([]string{"a", "b", "c"}, ",")  // a,b,c

	//// CRUD(Create Read Update Delete) on slice by index
	//fib := []int{1, 1, 2, 3, 5, 8}
	//r, _ := huge.InsertSliceE(fib, 6, 13)    // [1 1 2 3 5 8 13]
	//r, _ := huge.DeleteSliceE(fib, 0)        // [1 2 3 5 8]
	//r, _ := huge.UpdateSliceE(fib, 5, 88)    // [1 1 2 3 5 88]
	//r, _ := huge.GetEleIndexesSliceE(fib, 1) // [0 1]
	//
	//// or
	//r := huge.InsertIntSlice(fib, 5, 13)  // [1 1 2 3 5 8 13]
	//r := huge.DeleteIntSliceE(fib, 0)     // [1 2 3 5 8]
	//r := huge.UpdateIntSliceE(fib, 5, 88) // [1 1 2 3 5 88]
	//r := huge.GetEleIndexesSlice(fib, 1)  // [0 1]

}
func IsNull(val interface{}) bool {
	isNull := false
	switch val.(type) {
	case string:
		isNull = len(val.(string)) == 0
	case int:
		isNull = val.(int) == 0
	case int32:
		isNull = val.(int32) == 0
	case int64:
		isNull = val.(int64) == 0
	case float32:
		isNull = val.(float32) == 0
	case float64:
		isNull = val.(float64) == 0
	}
	return isNull
}

//单词全部转化为大写
func ToUpper(s string) string {
	return strings.ToUpper(s)
}

//单词全部转化为小写
func ToLower(s string) string {
	return strings.ToLower(s)
}

//下划线单词转为大写驼峰单词
func UderscoreToUpperCamelCase(s string) string {
	s = strings.Replace(s, "_", " ", -1)
	s = strings.Title(s)
	return strings.Replace(s, " ", "", -1)
}

//下划线单词转为小写驼峰单词
func UderscoreToLowerCamelCase(s string) string {
	s = UderscoreToUpperCamelCase(s)
	return string(unicode.ToLower(rune(s[0]))) + s[1:]
	return s
}

//驼峰单词转下划线单词
func CamelCaseToUdnderscore(s string) string {
	var output []rune
	for i, r := range s {
		if i == 0 {
			output = append(output, unicode.ToLower(r))
		} else {
			if unicode.IsUpper(r) {
				output = append(output, '_')
			}

			output = append(output, unicode.ToLower(r))
		}
	}
	return string(output)
}

func GetUUID() string {
	u := uuid.NewV4()
	return strings.ReplaceAll(u.String(), "-", "")
}
